﻿using FS.Common.DotNetBean;
using FS.Common.DotNetCode;
using FS.Common.DotNetUI;
using FS.Web;
using FS.Business;
using System;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;
using System.Text;

namespace FS.Web.BLL.Visa_Base
{
    public partial class Visa_infoSupplyForm : PageBase
    {
        private string _key;
        visa_dao _visa = new visa_dao();
        protected void Page_Load(object sender, EventArgs e)
        {
            this._key = base.Request["key"];
            if (!base.IsPostBack)
            {
                this.GetSelect_TrustInfo();
                this.GetSelect_InfoFlag();               
                initBase_postby();
                initBase_moneytype();
                initBase_moneyflag();

                //设置签证时间
                this.info_getdate.Value = DateTime.Now.ToString("yyyy-MM-dd");

                if (!string.IsNullOrEmpty(this._key))               
                {                   
                    this.InitData();
                    DataBindGrid();
                }
            }
        }

        #region 初始项目数据绑定
        //委托方
        private void GetSelect_TrustInfo()
        {
            DataTable dt = _visa.GetSelect_TrustInfo();
            ControlBindHelper.BindHtmlSelect(dt, this.trust_id, "name", "trust_id", "请选择");
        }

        //签证状态
        private void GetSelect_InfoFlag()
        {
            DataTable dt = _visa.GetSelect_InfoFlag();
            ControlBindHelper.BindHtmlSelect(dt, this.Ref_infoflag, "property_value", "property_key", "");
        }

        //取件类型
        private void initBase_postby()
        {
            DataTable dt = _visa.GetSelect_postby();
            ControlBindHelper.BindHtmlSelect(dt, this.Ref_postby, "property_value", "property_key", "");
        }

        //收款方式
        private void initBase_moneytype()
        {
            DataTable dt = _visa.GetSelect_moneytype();
            ControlBindHelper.BindHtmlSelect(dt, this.Ref_moneytype, "property_value", "property_key", "");
        }

        //收款情况
        private void initBase_moneyflag()
        {
            DataTable dt = _visa.GetSelect_moneyflag();
            ControlBindHelper.BindHtmlSelect(dt, this.Ref_moneyflag, "property_value", "property_key", "");
        }

        #endregion

        private void InitData()
        {
            Hashtable ht = DataFactory.Run().GetHashtableById("Visa_Info", "Info_ID", this._key);
            if (ht.Count > 0 && ht != null)
            {
                ht["INFO_GETDATE"] = (Convert.ToDateTime(ht["INFO_GETDATE"])).ToString("yyyy-MM-dd");

                string ls_trust = ht["TRUST_ID"].ToString();
                DataTable dt = _visa.GetSelect_trustInfoBytrustid(ls_trust);
                if (dt.Rows.Count > 0)
                {
                    ht["Ref_pricetype"] = dt.Rows[0]["type_price"].ToString();
                    ht["Ref_trusttype"] = dt.Rows[0]["type_trust"].ToString();
                }
                ControlBindHelper.SetWebControls(this.Page, ht);
            }
        }

        private void DataBindGrid()
        {            
            DataTable dtsum = _visa.GetTotalInfoDetail(this._key);
            
            string ls_people = "0";
            string ls_money = "0";
            string ls_post = this.Ref_postmoney.Value.ToString();

            if (dtsum.Rows.Count > 0)
            {
                ls_people = dtsum.Rows[0]["totalpeo"].ToString();
                ls_money = dtsum.Rows[0]["price"].ToString();
            }
            this.info_outpeople.Value = ls_people;
            this.info_outmoney.Value = ls_money;
            this.info_moneyin.Value = Convert.ToString(Convert.ToDecimal(ls_money) + Convert.ToDecimal(ls_post));

            this.bind();
        }

       
        protected void Save_Click(object sender, EventArgs e)
        {
            ShowMsgHelper.AlertMsg("操作成功！");
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)//1先判断当前行是不是数据行
            {
                if (e.Row.RowState == DataControlRowState.Edit ||
                  e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
                {
                                        
                    DropDownList ddlSupply = e.Row.FindControl("ddl_supply") as DropDownList;
                    string ls_supply = ((HiddenField)e.Row.FindControl("Hd_supply")).Value;
                    string ls_city = ((HiddenField)e.Row.FindControl("Hd_city")).Value;
                    string ls_certtype = ((HiddenField)e.Row.FindControl("Hd_certtype")).Value;
                    DataTable dt = _visa.GetSelect_SupplyInfoByCityIDAndCertType(ls_city, ls_certtype);
                    if (ddlSupply != null)
                    {
                        ddlSupply.DataSource = dt;
                        ddlSupply.DataTextField = "supplyPrice";
                        ddlSupply.DataValueField = "Supply_id";
                        ddlSupply.DataBind();

                        if (ddlSupply.Items.Count > 0)
                        {                           
                            ddlSupply.Items.FindByValue(ls_supply).Selected = true; 
                        }
                    }
                }
            } 
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            bind();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            
            string sCustomerName = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txt_infodetail_user"))).Text;
            string ls_supply = ((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("ddl_supply"))).Text.ToString().Trim();
            string ls_price = ((Label)GridView1.Rows[e.RowIndex].FindControl("lb_sprice")).Text;
            string infodetailid = GridView1.DataKeys[e.RowIndex].Value.ToString();
            string ls_username = RequestSession.GetSessionUser().UserName.ToString();
            string ls_userid = RequestSession.GetSessionUser().UserId.ToString();

            bool isok=_visa.UpdateDetailSupplyAndCustomer(infodetailid,ls_supply,sCustomerName,ls_price, ls_userid, ls_username);
            if (!isok)
            {
                ShowMsgHelper.Alert_Error("操作失败！");
            }
            else {
                //重新统计价格
                DataTable dtsum = _visa.GetTotalInfoDetail(this._key);
                                
                string ls_cost = "0";                
                if (dtsum.Rows.Count > 0)
                {
                    ls_cost = dtsum.Rows[0]["cost"].ToString();
                }
                this.info_outprice.Value = ls_cost;
                this.info_moneyout.Value = ls_cost;
                
                GridView1.EditIndex = -1;
                bind();
            }

            GridView1.EditIndex = -1;
            bind();            
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            bind();
        }

        public void bind()
        {
            DataTable dt = _visa.GetInfoDetail(this._key);
            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();
        }

        public void ddlsupply_SelectedIndexChanged(object sender, EventArgs e)
        {

            DropDownList ddlsupply = (DropDownList)sender;//获取现在的事件触发者：乡的DropDownList
            GridViewRow gvr = (GridViewRow)ddlsupply.NamingContainer;//乡与村同属于在一个NamingContainer下
            string ls_txt = ddlsupply.SelectedItem.Text;
            string ls_price = ls_txt.Substring(ls_txt.IndexOf(':') + 1);

            ((Label)gvr.FindControl("lb_sprice")).Text=ls_price;
        }
      
    }
}